using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace Pg.BioMedics.RatObserver.Gui.Controls
{
    public class StringListBox : Panel
    {
        #region Fields

        private readonly ListBox _listBox;
        private readonly MenuItem _addMenuItem;
        private readonly MenuItem _editMenuItem;
        private readonly MenuItem _removeMenuItem;

        private string[] _data;

        #endregion

        #region Constructors

        /// <summary>
        /// Creates instance of StringListBox
        /// </summary>
        public StringListBox()
            : base()
        {
            _editMenuItem = new MenuItem("Edit", new EventHandler(ListBox_DoubleClick));
            _editMenuItem.Visible = false;

            _addMenuItem = new MenuItem("Add", new EventHandler(AddMenuItem_Click));
            _addMenuItem.Shortcut = Shortcut.Ins;
            _addMenuItem.ShowShortcut = true;

            _removeMenuItem = new MenuItem("Remove", new EventHandler(RemoveMenuItem_Click));
            _removeMenuItem.Visible = false;
            _removeMenuItem.Shortcut = Shortcut.Del;
            _removeMenuItem.ShowShortcut = true;

            _listBox = new ListBox();
            _listBox.Dock = DockStyle.Fill;
            _listBox.ContextMenu = new ContextMenu(new MenuItem[] { _editMenuItem, _addMenuItem, _removeMenuItem });
            _listBox.SelectedIndexChanged += new EventHandler(ListBox_SelectedIndexChanged);
            _listBox.DoubleClick += new EventHandler(ListBox_DoubleClick);

            Controls.Add(_listBox);
        }                               

        #endregion

        #region Properties

        public string[] Values
        {
            get
            {
                string[] values = new string[_listBox.Items.Count];
                for (int i = 0; i < values.Length; i++)
                    values[i] = (string)_listBox.Items[i];

                return values;
            }

            set
            {
                if (value == _data)
                    return;

                _listBox.Items.Clear();
                _listBox.Items.AddRange(value);

                _data = value;
            }
        }

        #endregion

        #region Event handlers

        private void AddMenuItem_Click(object sender, EventArgs e)
        {
            InputForm dialog = new InputForm();
            if (dialog.ShowDialog() == DialogResult.OK)
                _listBox.Items.Add(dialog.Value);
        }

        private void RemoveMenuItem_Click(object sender, EventArgs e)
        {
            _listBox.Items.Remove(_listBox.SelectedItem);
        }

        private void ListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            _editMenuItem.Visible = (_listBox.SelectedIndex != -1);
            _removeMenuItem.Visible = (_listBox.SelectedIndex != -1);            
        }

        private void ListBox_DoubleClick(object sender, EventArgs e)
        {
            if (_listBox.SelectedIndex != -1)
            {
                InputForm dialog = new InputForm();
                if (dialog.ShowDialog((string)_listBox.SelectedItem) == DialogResult.OK)
                {
                    int index = _listBox.SelectedIndex;

                    _listBox.Items.RemoveAt(index);
                    _listBox.Items.Insert(index, dialog.Value);
                }
            }
        }

        #endregion
    }
}
